package com.liuxu.topnews.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.liuxu.topnews.BuildConfig;
import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.R;
import com.liuxu.topnews.bean.App;
import com.liuxu.topnews.utils.AppUtils;
import com.liuxu.topnews.utils.ShareUtil;
import com.liuxu.topnews.utils.ToastUtils;

import java.util.List;

import static com.liuxu.topnews.MyApplication.globalapps;

public class Recycle_Applist_Adapter extends RecyclerView.Adapter<Recycle_Applist_Adapter.MyHolder> {
    Context context;
    public  int opened=-1;
    public final int swipe=0;
    public final int key=0;
    public final int as=0;

    public Recycle_Applist_Adapter(){
        this.context=MyApplication.getContext();
    }

    public void update(){
        notifyDataSetChanged();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view=null;
        switch (BuildConfig.version) {
            case "normal":
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.applist_item,viewGroup,false);
                break;
            case "hw":
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.applist_item_hw,viewGroup,false);
                break;
            case "xm":
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.applist_item_hw,viewGroup,false);
                break;
            default:
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.applist_item,viewGroup,false);
                break;
        }
        MyHolder myHolder=new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder myHolder, final int i) {

        myHolder.app_id.setText(globalapps.get(i).getId() + ".");
        myHolder.app_name.setText(globalapps.get(i).getZname());
        myHolder.js_version_code.setText(globalapps.get(i).getJsversion());
        myHolder.uptime.setText(String.valueOf(globalapps.get(i).getUptime()));
        myHolder.downtime.setText(String.valueOf(globalapps.get(i).getDowntime()));
        myHolder.get_money.setText(String.valueOf(globalapps.get(i).getGetMoney()));
        myHolder.read_count.setText(String.valueOf(globalapps.get(i).getPagenumber()));
        myHolder.max_read_count.setText(String.valueOf(globalapps.get(i).getMax_read()));
        myHolder.now_read_count.setText(String.valueOf(ShareUtil.getInstance().AppReadCountGet(globalapps.get(i))));
        myHolder.now_use_time.setText((ShareUtil.getInstance().AppUseTimeGet(globalapps.get(i))));
        if (myHolder.setuptime.getTag() instanceof TextWatcher){
            myHolder.setuptime.removeTextChangedListener ((TextWatcher) myHolder.setuptime.getTag());
        }
        if (myHolder.setdowntime.getTag() instanceof TextWatcher){
            myHolder.setdowntime.removeTextChangedListener ((TextWatcher) myHolder.setdowntime.getTag());
        }
        if (myHolder.setread_count.getTag() instanceof TextWatcher){
            myHolder.setread_count.removeTextChangedListener ((TextWatcher) myHolder.setread_count.getTag());
        }
        if (myHolder.setmax_read_count.getTag() instanceof TextWatcher){
            myHolder.setmax_read_count.removeTextChangedListener ((TextWatcher) myHolder.setmax_read_count.getTag());
        }
        if (myHolder.setmax_read_count.getTag() instanceof TextWatcher){
            myHolder.setmax_read_count.removeTextChangedListener ((TextWatcher) myHolder.setmax_read_count.getTag());
        }
        myHolder.homespinner.setOnCheckedChangeListener(null);
        myHolder.newsspinner.setOnCheckedChangeListener(null);

        myHolder.swipe.setId(0);
        myHolder.key.setId(1);
        myHolder.page.setId(2);
        myHolder.event.setId(3);
        myHolder.swipe1.setId(0);
        myHolder.key1.setId(1);
        myHolder.page1.setId(2);
        myHolder.event1.setId(3);

        myHolder.setuptime.setText(String.valueOf(globalapps.get(i).getUptime()));
        myHolder.setdowntime.setText(String.valueOf(globalapps.get(i).getDowntime()));
        myHolder.setread_count.setText(String.valueOf(globalapps.get(i).getPagenumber()));
        myHolder.setmax_read_count.setText(String.valueOf(globalapps.get(i).getMax_read()));
        myHolder.homespinner.check(globalapps.get(i).movetype);
        myHolder.newsspinner.check(globalapps.get(i).readtype);
        TextWatcher setuptime=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")){
                    globalapps.get(i).setUptime(Integer.parseInt(s.toString()));
                }
            }
        };
        TextWatcher setdowntime=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")){
                    globalapps.get(i).setDowntime(Integer.parseInt(s.toString()));
                }
            }
        };
        TextWatcher setread_count=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")){
                    globalapps.get(i).setPagenumber(Integer.parseInt(s.toString()));
                }
            }
        };
        TextWatcher setmax_read_count=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")){
                    globalapps.get(i).setMax_read(Integer.parseInt(s.toString()));
                }
            }
        };
        myHolder.setuptime.addTextChangedListener(setuptime);
        myHolder.setdowntime.addTextChangedListener(setdowntime);
        myHolder.setread_count.addTextChangedListener(setread_count);
        myHolder.setmax_read_count.addTextChangedListener(setmax_read_count);
        myHolder.setuptime.setTag(setuptime);
        myHolder.setdowntime.setTag(setdowntime);
        myHolder.setread_count.setTag(setread_count);
        myHolder.setmax_read_count.setTag(setmax_read_count);

        myHolder.install.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.v("打开商店",globalapps.get(i).packname);
                AppUtils.goToMarket(globalapps.get(i).packname);
            }
        });
        myHolder.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PackageManager var2 = MyApplication.getContext().getPackageManager();
                Intent intent = var2.getLaunchIntentForPackage(globalapps.get(i).packname);
                MyApplication.getContext().startActivity(intent);
            }
        });
        myHolder.homespinner.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton button = (RadioButton) group.findViewById(checkedId);
                String b= button.getText().toString();
                ToastUtils.getInstance().shownow(globalapps.get(i).zname+"主页选择"+b+"模式滑动");
                globalapps.get(i).setMovetype(checkedId);
            }
        });
        myHolder.newsspinner.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton button = (RadioButton) group.findViewById(checkedId);
                String b= button.getText().toString();
                ToastUtils.getInstance().shownow(globalapps.get(i).zname+"文章页选择"+b+"模式滑动");
                globalapps.get(i).setReadtype(checkedId);
            }
        });
        if (globalapps.get(i).IsInstall) {
            myHolder.app_icon.setImageDrawable(globalapps.get(i).icon);
            myHolder.version_code.setText(String.valueOf(globalapps.get(i).getVersionName()));
            myHolder.install.setVisibility(View.GONE);
            myHolder.open.setVisibility(View.VISIBLE);
        }else {
            myHolder.app_icon.setImageResource(R.drawable.icon_def_app);
            myHolder.version_code.setText("未安装");
            myHolder.install.setVisibility(View.VISIBLE);
            myHolder.open.setVisibility(View.GONE);
        }
        if (globalapps.get(i).IsHot) {
            myHolder.hot_img.setVisibility(View.VISIBLE);
        }else {
            myHolder.hot_img.setVisibility(View.GONE);
        }
        if (i == opened){
            myHolder.top_layout1.setVisibility(View.VISIBLE);
        } else{
            myHolder.top_layout1.setVisibility(View.GONE);
        }
        myHolder.top_layout0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (opened == i) {
                    opened = -1;
                    notifyItemChanged(i);
                } else {
                    int oldOpened = opened;
                    opened = i;
                    notifyItemChanged(oldOpened);
                    notifyItemChanged(opened);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return globalapps.size();
    }

    class MyHolder extends RecyclerView.ViewHolder{
        TextView app_id;
        ImageView app_icon;
        ImageView hot_img;
        TextView app_name;
        TextView version_code;
        TextView js_version_code;
        TextView uptime;
        TextView downtime;
        TextView get_money;
        TextView use_time;
        TextView max_read_count;
        TextView read_count;
        TextView now_read_count;
        TextView now_use_time;
        Button install;
        Button open;
        LinearLayout top_layout0;
        LinearLayout top_layout1;
        EditText setuptime;
        EditText setdowntime;
        EditText setread_count;
        EditText setmax_read_count;
        RadioGroup homespinner;
        RadioGroup newsspinner;
        RadioButton swipe;
        RadioButton key;
        RadioButton event;
        RadioButton page;
        RadioButton swipe1;
        RadioButton key1;
        RadioButton event1;
        RadioButton page1;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            app_icon=itemView.findViewById(R.id.appicon);
            hot_img=itemView.findViewById(R.id.hot_img);
            app_id=itemView.findViewById(R.id.app_id);
            app_name=itemView.findViewById(R.id.app_name);
            version_code=itemView.findViewById(R.id.version_code);
            js_version_code=itemView.findViewById(R.id.js_version_code);
            uptime=itemView.findViewById(R.id.uptime);
            downtime=itemView.findViewById(R.id.downtime);
            get_money=itemView.findViewById(R.id.get_money);
            use_time=itemView.findViewById(R.id.use_time);
            max_read_count=itemView.findViewById(R.id.max_read_count);
            read_count=itemView.findViewById(R.id.read_count);
            now_read_count=itemView.findViewById(R.id.now_read_count);
            now_use_time=itemView.findViewById(R.id.now_use_time);
            install=itemView.findViewById(R.id.install);
            open=itemView.findViewById(R.id.open);
            top_layout0=itemView.findViewById(R.id.top_layout0);
            top_layout1=itemView.findViewById(R.id.top_layout1);
            setuptime=itemView.findViewById(R.id.setuptime);
            setdowntime=itemView.findViewById(R.id.setdowntime);
            setread_count =itemView.findViewById(R.id.setread_count);
            setmax_read_count =itemView.findViewById(R.id.setmax_read_count);
            homespinner =itemView.findViewById(R.id.homespinner);
            newsspinner =itemView.findViewById(R.id.newsspinner);
            swipe =itemView.findViewById(R.id.swipe);
            key =itemView.findViewById(R.id.key);
            event =itemView.findViewById(R.id.as);
            page =itemView.findViewById(R.id.page);
            swipe1 =itemView.findViewById(R.id.swipe1);
            key1 =itemView.findViewById(R.id.key1);
            event1 =itemView.findViewById(R.id.as1);
            page1 =itemView.findViewById(R.id.page1);
        }
    }
}
